package com.example.mvvmdemo.data.db

import com.example.mvvmdemo.data.db.dao.HomeDao
import com.example.mvvmdemo.data.entity.BannerBean
import com.example.mvvmdemo.http.NetWork
import com.yangk.mvvm.base.BaseModel

/**
 * <pre>
 *     author : Yangk
 *     time   : 2021/11/15
 *     desc   : 数据仓库
 * </pre>
 */
class  Repository private constructor(
    private val netWork: NetWork,
    private val localData: HomeDao
) : BaseModel(){

    companion object {

        @Volatile
        private var INSTANCE: Repository? = null

        fun getInstance(netWork: NetWork, homeDao: HomeDao) =
            INSTANCE ?: synchronized(this) {
                INSTANCE ?: Repository(netWork, homeDao).also { INSTANCE = it }
            }
    }

    suspend fun getBannerData(refresh: Boolean = false): List<BannerBean> {
        return cacheNetCall({
            netWork.getBannerData()
        }, {
            localData.getBannerList()
        }, {
            if (refresh)localData.deleteBannerAll()
                localData.insertBanner(it)
        }, {
            !refresh && !it.isNullOrEmpty()
        })
    }


}